﻿using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using CRMG.ResourceManagement.Resources;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using Telerik.WinControls.UI.Data;

namespace CRMG.Container.Views.Training
{
    public partial class FrmStudentTrain : Telerik.WinControls.UI.RadForm
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        StudentTrain stdntTrain;
        TrainManager trainMan;
        DataTable dtStudentTrains;
        int _studentId = -1;

        public FrmStudentTrain(int studentId, object studentFullName)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmStudentTrain).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmStudentTrain).Assembly);
                trainMan = new TrainManager();
                stdntTrain = new StudentTrain();
                _studentId = studentId;
                lblSelectedStudent.Text = string.Format(formRes.GetString("FrmStudentTrain.lblSelectedStudent.Format"), studentFullName != null ? studentFullName.ToString() : "");
                lblTrain.Text = formRes.GetString("FrmStudentTrain.lblTrain");
                lblTrainType.Text = formRes.GetString("FrmStudentTrain.lblTrainType");
                btnSave.Text = formRes.GetString("Save");
                btnCancel.Text = formRes.GetString("Cancel");
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmStudentTrain.Ctor.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2016,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2016)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudentTrain.Ctor.Error"),
                    messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
            }
        }

        private void FormLoad(object sender, EventArgs e)
        {
            try
            {
                dtStudentTrains = trainMan.GetStudentTrains(_studentId);
                ControlUtil.LoadDataTable2DropDownList(drpTrain, trainMan.GetAvailableTrains(), "TrainName", "OBJID");
                ControlUtil.LoadDataTable2DropDownList(drpTrainType, trainMan.GetTrainTypes(), "TrainTypeName", "OBJID");
                stdntTrain = new StudentTrain() { StudentId = _studentId };
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmStudentTrain.Load.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2017,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2017)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudentTrain.Load.Error"),
                    messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
            }
        }

        private void TrainChange(object sender, PositionChangedEventArgs e)
        {
            stdntTrain.AvailableTrainId = ConverterUtil.Convert2Int(drpTrain.SelectedValue);
            btnSave.Enabled = saveControl();
        }

        private bool saveControl()
        {
            return ((stdntTrain.TrainTypeId * stdntTrain.AvailableTrainId) > 0) == true;
        }

        private void TrainTypeChange(object sender, PositionChangedEventArgs e)
        {
            stdntTrain.TrainTypeId = drpTrainType.SelectedValue.ToInt();
            btnSave.Enabled = saveControl();
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                object obj = ControlUtil.GetObjectWithRefColumnAndRefValue(dtStudentTrains,
                    "TrainTypeId", "AvailableTrainId", stdntTrain.AvailableTrainId);
                int pTrainTypeId = ConverterUtil.Convert2Int(obj);
                if (pTrainTypeId == stdntTrain.TrainTypeId)
                {
                    ControlUtil.ShowErrorMessage(
                     messageRes.GetString("FrmStudentTrain.Save.AlreadyExist"),
                     messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    int Id = trainMan.AddStudentTrain(stdntTrain);
                    ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudentTrain.Save.Success"),
                    messageRes.GetString("Info"));
                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = TransactionTypes.Insert,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag != null ? btnSave.Tag.ToString() : "",
                        LogObject = Id
                    };
                    transLogger.Insert();
                }

            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmStudentTrain.Save.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2018,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2018)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmStudentTrain.Save.Error"),
                    messageRes.GetString("Error"));
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
